package com.intel.factorytest.activity;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.intel.factorytest.R;

public class Audio extends BaseActivity implements OnClickListener {
    Button playButton;
    Button pauseButton;
    boolean isPlaying = true;
    MediaPlayer mediaPlayer;

    private TextView durationTime = null;
    private TextView currentTime = null;
    private TextView audioTextView = null;
    private static final int UPDATE = 1;
    private Timer mTimer;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.audio);
        currentTime = (TextView) findViewById(R.id.currentTime);
        durationTime = (TextView) findViewById(R.id.totalTime);
        playButton = (Button) findViewById(R.id.playButton);
        playButton.setOnClickListener(this);
        pauseButton = (Button) findViewById(R.id.pauseButton);
        pauseButton.setOnClickListener(this);
        getWindow().setFormat(PixelFormat.UNKNOWN);
        mediaPlayer = new MediaPlayer();
        audioTextView = (TextView) findViewById(R.id.audioView);
        audioTextView
                .setText(getString(R.string.SERVICEMENU_AUDIO_PLAYING_TXT));
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        playButton.setText(getString(R.string.SERVICEMENU_VIDEO_PAUSE_TXT));
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
        mp.release();
        mediaPlayer = null;
        finish();
        }
        });

    }

    private Uri getMediaVolumeUri(Context context) {
        return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
                + context.getPackageName() + "/" + R.raw.audio);
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == UPDATE) {
                if (mediaPlayer == null)
                    return;
                int position = mediaPlayer.getCurrentPosition() / 1000;
                int total = mediaPlayer.getDuration() / 1000;
                currentTime.setText("00:0" + position);
                durationTime.setText("00:0" + total);
            }

        }
    };

    @Override
    protected void onResume() {
        Log.i("meilianggang", "onResume");
        super.onResume();
        if (mediaPlayer != null) {
            try {
                mediaPlayer.setDataSource(this, getMediaVolumeUri(this));
                mediaPlayer.prepare();
                mediaPlayer.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        mTimer = new Timer();
        mTimer.schedule(new TimerTask() {

            @Override
            public void run() {
                mHandler.sendEmptyMessage(UPDATE);
            }

        }, 500, 500);
    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    @Override
    protected void onDestroy() {
        if (mediaPlayer != null)
            mediaPlayer.release();
        super.onDestroy();

    }

    public void onClick(View v) {
        if (v == playButton) {
            if (isPlaying == true) {
                Log.i("meilianggang", "mediaPlayer.pause()");
                if (mediaPlayer != null) {
                    if (!mediaPlayer.isPlaying() == true) {
                        return;
                    }
                } else {
                    return;
                }
                mediaPlayer.pause();
                playButton
                        .setText(getString(R.string.SERVICEMENU_VIDEO_PLAY_TXT));
                audioTextView
                        .setText(getString(R.string.SERVICEMENU_AUDIO_PAUSE_TXT));
                isPlaying = false;
            } else {
                Log.i("meilianggang", "mediaPlayer.start()");
                if (mediaPlayer != null) {
                    if (mediaPlayer.isPlaying() == true) {
                        return;
                    }
                } else {
                    return;
                }
                mediaPlayer.start();
                playButton
                        .setText(getString(R.string.SERVICEMENU_VIDEO_PAUSE_TXT));
                audioTextView
                        .setText(getString(R.string.SERVICEMENU_AUDIO_PLAYING_TXT));
                isPlaying = true;
            }
        } else if (v == pauseButton) {
            finish();
        }
    }

    @Override
    public void finish() {
        mHandler.removeMessages(UPDATE);
        mTimer.cancel();
        setTestResult();
        super.finish();
    }
}
